package com.city.parking;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.search.core.CityInfo;
import com.baidu.mapapi.search.core.PoiInfo;
import com.baidu.mapapi.search.core.SearchResult;
import com.baidu.mapapi.search.poi.OnGetPoiSearchResultListener;
import com.baidu.mapapi.search.poi.PoiCitySearchOption;
import com.baidu.mapapi.search.poi.PoiDetailResult;
import com.baidu.mapapi.search.poi.PoiNearbySearchOption;
import com.baidu.mapapi.search.poi.PoiResult;
import com.baidu.mapapi.search.poi.PoiSearch;
import com.city.parking.map.OfflineMapSettings;
import com.city.parking.provider.ParkingProviderHelper;
import com.city.parking.provider.ParkingProviderHelper.Car;
import com.city.parking.util.SharePrefUtil;
import com.city.parking.util.Utils;

import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.widget.CursorAdapter;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class MyCarsActivity extends FragmentActivity implements OnClickListener {
    private CarListAdapter mAdapter;
    private Cursor mCursor = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_cars);
		TextView titleTv = (TextView) findViewById(R.id.nav_title);
		titleTv.setText("我的爱车");

        ImageView back = (ImageView) findViewById(R.id.nav_ico_back);
        back.setOnClickListener(this);

        ImageView more = (ImageView) findViewById(R.id.nav_ico_more);
        more.setImageResource(R.drawable.nav_ico_add);
        more.setOnClickListener(this);
        
        mCursor = ParkingProviderHelper.getAllCars(this);
        ListView listView = (ListView) findViewById(R.id.my_cars_list);
        mAdapter = new CarListAdapter(this, mCursor);
        listView.setAdapter(mAdapter);
        listView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View v, int pos,
                    long arg3) {
                // TODO Auto-generated method stub
                ViewHolder holder = (ViewHolder) v.getTag();
                if (holder.isDft)
                	return;
				LinearLayout ll = holder.wrap_ll;
				if (ll.getVisibility() == View.VISIBLE) {
					ll.setVisibility(View.GONE);
					holder.arrow.setImageResource(R.drawable.ico_arrow_down);
				} else {
					ll.setVisibility(View.VISIBLE);
					holder.arrow.setImageResource(R.drawable.ico_arrow_up);
				}
            }

        });
        if (mCursor == null || mCursor.getCount() == 0) {
            Toast.makeText(this, "请先添加您的爱车", Toast.LENGTH_SHORT).show();
            Intent i = new Intent(this, AddCarActivity.class);
            startActivityForResult(i, ParkingApp.RQST_CODE_ADD_CAR);
        }
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
        case R.id.nav_ico_back:
            finish();
            break;
        case R.id.nav_ico_more:
            Intent i = new Intent(this, AddCarActivity.class);
            startActivityForResult(i, ParkingApp.RQST_CODE_ADD_CAR);
            break;

        default:
            break;
        }
    }
    
    @Override
    public void onActivityResult(int requestCode, int resultCode,
            Intent intent) {
        if (requestCode == ParkingApp.RQST_CODE_ADD_CAR 
                && resultCode == RESULT_OK) {
            Bundle data = intent.getExtras();
            boolean big = data.getBoolean("big");
            String car_no = data.getString("car_no");

            Utils.log("add car " + car_no + ", big=" + big);
            int dft = 0;

            if (mCursor == null || mCursor.getCount() == 0)
            	dft = 1;
            ParkingProviderHelper.addCar(this, car_no, big ? 1 : 0, dft);
            if (dft == 1) {
				SharePrefUtil.setCarId(car_no);
				ParkingApp.mCarId = car_no;
            }
            if (mCursor == null) {
                mCursor = ParkingProviderHelper.getAllCars(this);
                mAdapter.changeCursor(mCursor);
            }
            mCursor.requery();
            Utils.log("all car =" + mCursor.getCount());
        }
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    private class ViewHolder {
        TextView carNo;
        TextView carTp;
        ImageView arrow;
        ImageView ico;
        LinearLayout wrap_ll;
        boolean isDft;
    }

    private class CarListAdapter extends CursorAdapter {
        private LayoutInflater inflater = null;

        public CarListAdapter(Context context, Cursor cursor) {
            super(context, cursor, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
            inflater = LayoutInflater.from(context);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            Cursor cursor = getCursor();
            if (cursor == null || !cursor.moveToPosition(position)) {
                throw new IllegalStateException(
                        "couldn't move cursor to position "
                                + position);
            }

            ViewHolder holder = null;
            if (convertView == null) {
                holder = new ViewHolder();
                convertView = inflater.inflate(R.layout.car_item, null);
				holder.carNo = (TextView) convertView.findViewById(R.id.car_no);
				holder.carTp = (TextView) convertView.findViewById(R.id.car_type);
				holder.arrow = (ImageView) convertView.findViewById(R.id.ico_arrow);
				holder.ico = (ImageView) convertView.findViewById(R.id.car_ico);
                
                holder.wrap_ll = (LinearLayout) convertView.findViewById(R.id.set_as_default_ll);
                holder.wrap_ll.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
			            String no = (String)v.getTag();
						int p = ParkingProviderHelper.setDefault(MyCarsActivity.this, no);
						if (p >= 0) {
							SharePrefUtil.setCarId(no);
							ParkingApp.mCarId = no;
						}
						Utils.log("click car " + no);
						v.setVisibility(View.GONE);
			            mCursor.requery();
					}
				});

                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            
            String car_no = cursor.getString(cursor.getColumnIndex(Car.COLUMN_NO));
            int is_big = cursor.getInt(cursor.getColumnIndex(Car.COLUMN_ISBIG));
            int is_def = cursor.getInt(cursor.getColumnIndex(Car.COLUMN_ISDEFAULT));
            holder.wrap_ll.setTag(car_no);

            if (is_big == 0) {
                holder.carTp.setText("小车");
                holder.ico.setImageResource(R.drawable.ico_car_small);
            } else {
                holder.carTp.setText("大车");
                holder.ico.setImageResource(R.drawable.ico_car_big);
            }

            holder.isDft = (is_def == 1);
            if (holder.isDft) {
            	holder.arrow.setVisibility(View.GONE);
                holder.carNo.setText(car_no + "(默认)");
            } else {
            	holder.arrow.setVisibility(View.VISIBLE);
                holder.carNo.setText(car_no);            	
            }

            return convertView;
        }

        @Override
        public void bindView(View arg0, Context arg1, Cursor arg2) {
            // TODO Auto-generated method stub
            
        }

        @Override
        public View newView(Context arg0, Cursor arg1, ViewGroup arg2) {
            // TODO Auto-generated method stub
            return null;
        }
    }
}
